package com.jlnx.controller.core;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.jlnx.dao.ChainEnterpriseDaoImpl;
import com.jlnx.dao.CoreEnterpriseDaoImpl;
import com.jlnx.dao.EnterpriseDaoImpl;
import com.jlnx.dao.FinanceReqDaoImpl;
import com.jlnx.dao.PayStatusDaoImpl;
import com.jlnx.dao.SupplyChainDaoImpl;
import com.jlnx.pojo.ChainEnterprise;
import com.jlnx.pojo.CoreEnterprise;
import com.jlnx.pojo.Enterprise;
import com.jlnx.pojo.FinanceReq;
import com.jlnx.pojo.SupplyChain;
import com.jlnx.pojo.SupplyChainEnterprise;

/*
 * 更新白名单
 */
@WebServlet("/AddWhiteListController")
public class AddWhiteListController extends HttpServlet {
	
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		this.doPost(request, response);
	}


	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

		//获取请求信息
		response.setContentType("application/json;charset=UTF-8;text/html");
		//设置请求编码模式
    	request.setCharacterEncoding("utf-8");
				
		 //获取session对象
		 HttpSession hs=request.getSession();
		 //获取登录账号
		 String coreEntNo = ((Enterprise)hs.getAttribute("enterprise")).getEntNo();
		 System.out.println( coreEntNo + ":发起白名单更新请求");
		 
	        String[] upstreamIds = request.getParameterValues("upstreamId");
	        //String[] upstreamNames = request.getParameterValues("upstreamName[]");
	        String[] downstreamIds = request.getParameterValues("downstreamId");
	        //String[] downstreamNames = request.getParameterValues("downstreamName[]");
			
        String message = "";//供应链
        if (upstreamIds != null) { // 检查上游企业ID是否为null
            for (String upstreamId : upstreamIds) { // 遍历上游企业ID数组
                message += upstreamId;
                message += '|';
            }
        }

        message += coreEntNo;

        if (downstreamIds != null) { // 检查下游企业ID是否为null
            for (String downstreamId : downstreamIds) { // 遍历下游企业ID数组
                message += '|';
                message += downstreamId;
            }
        }
        
        
		SupplyChainDaoImpl supplydao = new SupplyChainDaoImpl();
		
        //判断是否存在相关供应链核心企业
        int i = supplydao.isExistChainListByEntNo(coreEntNo);
		if(i == 0) {//找不到核心企业，插入新供应链
			SupplyChain supplyChain = new SupplyChain(0, message, coreEntNo);
			if(supplydao.insertSupplyChain(supplyChain) < 0 ) {
				 request.setAttribute("isRequestSuccess", false);
				 request.setAttribute("message", "更新失败!");
				//跳转界面
				request.getRequestDispatcher("/ShowWhiteListController").forward(request, response);
			}else {
				 request.setAttribute("isRequestSuccess", true);
				 request.setAttribute("message", "白名单更新成功!");
				//跳转界面
				request.getRequestDispatcher("/ShowWhiteListController").forward(request, response);
			}
		}else if(i > 0) {//能找到核心企业，更新新供应链
			SupplyChain supplyChain = new SupplyChain(i, message, coreEntNo);
			if(supplydao.updateSupplyChain(supplyChain) < 0 ) {
				 request.setAttribute("isRequestSuccess", false);
				 request.setAttribute("message", "更新失败!");
				//跳转界面
				request.getRequestDispatcher("/ShowWhiteListController").forward(request, response);
			}else {
				 request.setAttribute("isRequestSuccess", true);
				 request.setAttribute("message", "白名单更新成功!");
				//跳转界面
				request.getRequestDispatcher("/ShowWhiteListController").forward(request, response);
			}
		}
				
	}

}
